package tr.edu.yildiz.ce.dap.appagent.command.prompt;

import java.util.StringTokenizer;

import tr.edu.yildiz.ce.dap.appagent.command.deployment.agent.LocalCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.AbstractAgentCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.CommandConstants;
import tr.edu.yildiz.ce.dap.appagent.exception.AgentDeploymentException;
import tr.edu.yildiz.ce.dap.appagent.exception.BasicAgentException;
import tr.edu.yildiz.ce.dap.appagent.manager.ApplicationManager;
import tr.edu.yildiz.ce.dap.appagent.model.AppDefinition;
import tr.edu.yildiz.ce.dap.appagent.model.DapMessage;
import tr.edu.yildiz.ce.dap.appagent.process.base.AbstractProcess;
import tr.edu.yildiz.ce.dap.appagent.util.DeploymentUtils;

public class LocalUninstallCommand extends BasicPromptCommand {

	public LocalUninstallCommand(AbstractProcess process) {
		super(process);
	}

	@Override
	protected AbstractAgentCommand prepareAgentCommand(AbstractProcess process,
			String commandStr) throws BasicAgentException {
		StringTokenizer sTokenizer = new StringTokenizer(commandStr," ");
		String command = sTokenizer.nextToken();
		String name = null;
		String version = null;
		while (sTokenizer.hasMoreElements()) {
			String pType = (String) sTokenizer.nextElement();
			if (pType.equals(CommandConstants.PARAMETER_TYPE.NAME)){
				name = sTokenizer.nextToken();
			}else if (pType.equals(CommandConstants.PARAMETER_TYPE.VERSION)){
				version = sTokenizer.nextToken();
			} 
		}
		AppDefinition appDefinition;
		try {
			appDefinition = ApplicationManager.getAppDefinition(name, version);
		} catch (Exception e) {
			throw new BasicAgentException(e);
		}
		
		uninstallComponents(appDefinition, process);
		return new LocalCommand(process);
	}
	
	private void uninstallComponents(AppDefinition appDefinition,AbstractProcess process) throws AgentDeploymentException{
		DeploymentUtils.uninstallApplication(appDefinition, process.getProcessManager().getContext());
	}

	@Override
	protected void processResponse(DapMessage response) {
		

	}

}
